package com.finace.money.common.util;

import android.content.Intent;

import com.finace.money.common.Constant;
import com.finace.money.data.bean.StatusListBean;
import com.finace.money.net.socket.ConnManager;
import com.finace.money.net.socket.PushReceiver;
import com.finace.money.ui.activity.LoginActivity;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.TreeMap;

/**
 * Created by zhangpeng on 2018/1/29.
 */

public class MessageSendUtil {

    private static String mAccount;
    private static Timer timer;

    public static void killTime() {
        if (null != timer) {
            timer.cancel();
        }
    }

    public static void HEARTBEAT() {
        //查询合约列表
        final TreeMap<String, String> map = new TreeMap<>();
        map.put("msgtype", "HEARTBEAT");
        map.put("payload", "");
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                LogUtil.d("connect", "handshakedata: " + "开始跳动");
                ConnManager.getInstance().sendMessage(ParamJsonUtil.getjsonData(map));
            }
        }, 5000, 5000);
    }

    public static void HeartBeatRequest() {
        //检测登录状态是否有效
        final TreeMap<String, String> map = new TreeMap<>();
        map.put("msgtype", "HeartBeatRequest");
        map.put("payload", "");
        ConnManager.getInstance().sendMessage(ParamJsonUtil.getjsonData(map));
    }

    public static void QueryProduct() {
        //查询合约列表
        //{"msgtype":"QueryProduct","payload":{"m_RequestID":}}
        TreeMap<String, String> twoMap = new TreeMap<>();
        twoMap.put("m_RequestID", "10");
        TreeMap<String, String> listMap = new TreeMap<>();
        listMap.put("msgtype", "QueryProduct");
        listMap.put("payload", ParamJsonUtil.getjsonData(twoMap));
        ConnManager.getInstance().sendMessage(ParamJsonUtil.getjsonData(listMap));
    }

    public static void QueryCandle(int mLevel, String mProductID) {
        //订阅历史行情
        TreeMap<String, String> twoMap = new TreeMap<>();
        twoMap.put("m_RequestID", UIUtil.getRandomRequestID(mLevel));
        twoMap.put("m_ProductID", mProductID);
        twoMap.put("m_Level", String.valueOf(mLevel));
        List<String> hisDate = DateUtil.getHistoryTime(mLevel);
        if (hisDate.size() == 2) {
            twoMap.put("m_BeginTime", hisDate.get(0));
            twoMap.put("m_EndTime", hisDate.get(1));
        }
        TreeMap<String, String> map = new TreeMap<>();
        map.put("msgtype", "QueryCandle");
        map.put("payload", ParamJsonUtil.getjsonData(twoMap));
        ConnManager.getInstance().sendMessage(ParamJsonUtil.getjsonData(map));
    }

    public static void RegisterSymbols() {
        //订阅实时行情
        List<StatusListBean> statusLists = (List<StatusListBean>) ACache.get(UIUtil.getContext()).getAsObject(PushReceiver.ACTION_LIST);
        StringBuilder stringBuilder = new StringBuilder();
        if (null != statusLists && statusLists.size() > 0) {
            for (int i = 0; i < statusLists.size(); i++) {
                if (i == statusLists.size() - 1) {
                    stringBuilder.append(statusLists.get(i).getM_Body().getM_ProductID());
                } else {
                    stringBuilder.append(statusLists.get(i).getM_Body().getM_ProductID()).append(",");
                }
            }
        }
        TreeMap<String, String> twoDetailMap = new TreeMap<>();
        twoDetailMap.put("symbols", stringBuilder.toString());
        TreeMap<String, String> mapDetail = new TreeMap<>();
        mapDetail.put("msgtype", "RegisterSymbols");
        mapDetail.put("payload", ParamJsonUtil.getjsonData(twoDetailMap));
        ConnManager.getInstance().sendMessage(ParamJsonUtil.getjsonData(mapDetail));
    }


    public static void UnregisterClient() {
        //请求退出交易{"msgtype":"UnregisterClient","payload":""}
        TreeMap<String, String> mapAccount = new TreeMap<>();
        mapAccount.put("msgtype", "UnregisterClient");
        mapAccount.put("payload", "");
        ConnManager.getInstance().sendMessage(ParamJsonUtil.getjsonData(mapAccount));
    }

    public static void QryAccount() {
        //查询资金{"msgtype":"QryAccount","payload":""}
        TreeMap<String, String> mapAccount = new TreeMap<>();
        mapAccount.put("msgtype", "QryAccount");
        mapAccount.put("payload", "");
        ConnManager.getInstance().sendMessage(ParamJsonUtil.getjsonData(mapAccount));
    }

    public static void QryOrder() {
        //查询委托{"msgtype":"QryOrder","payload":""}
        TreeMap<String, String> mapOrder = new TreeMap<>();
        mapOrder.put("msgtype", "QryOrder");
        mapOrder.put("payload", "");
        ConnManager.getInstance().sendMessage(ParamJsonUtil.getjsonData(mapOrder));
    }

    public static void QryTrade() {
        //查询成交{"msgtype":"QryTrade","payload":""}
        TreeMap<String, String> mapTrade = new TreeMap<>();
        mapTrade.put("msgtype", "QryTrade");
        mapTrade.put("payload", "");
        ConnManager.getInstance().sendMessage(ParamJsonUtil.getjsonData(mapTrade));
    }

    public static void QryPosition() {
        //查询持仓{"msgtype":"QryPosition","payload":""}
        TreeMap<String, String> mapPosition = new TreeMap<>();
        mapPosition.put("msgtype", "QryPosition");
        mapPosition.put("payload", "");
        ConnManager.getInstance().sendMessage(ParamJsonUtil.getjsonData(mapPosition));
    }

    public static void QryContractBank() {
        //查询银行{"msgtype":"QryContractBank","payload":""}
        TreeMap<String, String> mapPosition = new TreeMap<>();
        mapPosition.put("msgtype", "QryContractBank");
        mapPosition.put("payload", "");
        ConnManager.getInstance().sendMessage(ParamJsonUtil.getjsonData(mapPosition));
    }

    public static void QryInvestor() {
        //查询是否绑定过银行卡{"msgtype":"QryInvestor","payload":{"Account":"1880001"}}
        if (isLogin()) {
            TreeMap<String, String> twoDetailMap = new TreeMap<>();
            twoDetailMap.put("Account", mAccount);
            TreeMap<String, String> mapPosition = new TreeMap<>();
            mapPosition.put("msgtype", "QryInvestor");
            mapPosition.put("payload", ParamJsonUtil.getjsonData(twoDetailMap));
            ConnManager.getInstance().sendMessage(ParamJsonUtil.getjsonData(mapPosition));
        }
    }

    public static void ChangeInvestor(String name, String idCard, String bankId, String bankAc) {
        //绑定银行卡{"msgtype":"ChangeInvestor","payload":{"Account":"1880001","Name":"姓名",
        // IDCard":"422111222222223333","BankID":"1","BankAC":"1234564565"}}
        if (isLogin()) {
            TreeMap<String, String> twoDetailMap = new TreeMap<>();
            twoDetailMap.put("Account", mAccount);
            twoDetailMap.put("Name", name);
            twoDetailMap.put("IDCard", idCard);
            twoDetailMap.put("BankID", bankId);
            twoDetailMap.put("BankAC", bankAc);
            TreeMap<String, String> mapPosition = new TreeMap<>();
            mapPosition.put("msgtype", "ChangeInvestor");
            mapPosition.put("payload", ParamJsonUtil.getjsonData(twoDetailMap));
            ConnManager.getInstance().sendMessage(ParamJsonUtil.getjsonData(mapPosition));
        }
    }


    public static void CashOperationRequest(String amount, String deposit) {
        //充值体现接口{"msgtype":"CashOperationRequest","payload":{"Account":"1880001","Amount":"5000","
        //EnumCashOperation":"Deposit","EnumCashOPSource":"Online","Recvinfo":"第三方支付"}}
        TreeMap<String, String> twoDetailMap = new TreeMap<>();
        twoDetailMap.put("Account", mAccount);
        twoDetailMap.put("Amount", amount);
        twoDetailMap.put("EnumCashOperation", deposit);
        twoDetailMap.put("EnumCashOPSource", "Online");
        twoDetailMap.put("Recvinfo", "第三方支付");
        TreeMap<String, String> mapPosition = new TreeMap<>();
        mapPosition.put("msgtype", "CashOperationRequest");
        mapPosition.put("payload", ParamJsonUtil.getjsonData(twoDetailMap));
        ConnManager.getInstance().sendMessage(ParamJsonUtil.getjsonData(mapPosition));
    }

    public static void QryTransferSerial(String BankID) {
        //查询出入金记录{"msgtype":"QryTransferSerial","payload":{"Account":"1880001","BankID":"1"}}
        if (isLogin()) {
            TreeMap<String, String> twoDetailMap = new TreeMap<>();
            twoDetailMap.put("Account", mAccount);
            twoDetailMap.put("BankID", BankID);
            TreeMap<String, String> mapPosition = new TreeMap<>();
            mapPosition.put("msgtype", "QryTransferSerial");
            mapPosition.put("payload", ParamJsonUtil.getjsonData(twoDetailMap));
            ConnManager.getInstance().sendMessage(ParamJsonUtil.getjsonData(mapPosition));
        }
    }

    public static void QrySettleInfo(String date) {
        //查询结算单{"msgtype":"QrySettleInfo","payload":{"AccountID":"1880004","TradingDay":"20280228"}}
        TreeMap<String, String> twoDetailMap = new TreeMap<>();
        twoDetailMap.put("AccountID", mAccount);
        twoDetailMap.put("TradingDay", date);
        TreeMap<String, String> mapPosition = new TreeMap<>();
        mapPosition.put("msgtype", "QrySettleInfo");
        mapPosition.put("payload", ParamJsonUtil.getjsonData(twoDetailMap));
        ConnManager.getInstance().sendMessage(ParamJsonUtil.getjsonData(mapPosition));
    }

    public static void LoginRequest(String account, String password) {
        //登录
        TreeMap<String, String> twoDetailMap = new TreeMap<>();
        twoDetailMap.put("username", account);
        twoDetailMap.put("password", password);
        TreeMap<String, String> mapDetail = new TreeMap<>();
        mapDetail.put("msgtype", "LoginRequest");
        mapDetail.put("payload", ParamJsonUtil.getjsonData(twoDetailMap));
        ConnManager.getInstance().sendMessage(ParamJsonUtil.getjsonData(mapDetail));
    }

    public static boolean isLogin() {
        mAccount = ACache.get(UIUtil.getContext()).getAsString(Constant.account);
        if (StringUtil.isNotNullString(mAccount)) {
            return true;
        } else {
            UIUtil.startActivity(new Intent(UIUtil.getContext(), LoginActivity.class));
            return false;
        }
    }
}
